package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.base.utils.SPHelper;

import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.MainActivity;
import casa.depesos.app.com.databinding.FragmentRefuseBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.FailInfoBean;
import casa.depesos.app.com.http.req.RiskFailReq;
import casa.depesos.app.com.it.Refreshable;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class RefuseFragment extends BannerBaseFragment implements Refreshable {

    FragmentRefuseBinding binding;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show("ACTION_QQ_005");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentRefuseBinding.inflate(inflater, container, false);
        binding.rela.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);

        binding.frame.tv.setText("Cuenta atrás");

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    private void update() {
        getName(binding.top);
        getUnRead(binding.top);
        getLink(binding.bottom);
        showMsg(binding.msg, binding.scroll);
        getInfo();
        getBanner(binding.img);
    }

    private void getInfo() {
        new RiskFailReq().req(new ReqCallback<FailInfoBean>() {
            @Override
            public void callback(boolean success, FailInfoBean bean) {
                if (success && bean.isValid()) {
                    startCountDown(binding.frame, bean.info.getCountDownTime());
                    SpannableString ss;
                    if (Constant.white) {
                        long tmp = System.currentTimeMillis() + Long.parseLong(bean.info.countDownTime);
                        String day = Utils.getDay(tmp + "");
                        String s = String.format(getString(R.string.retry_next_time), day);
                        ss = new SpannableString(s);
                        int start = s.indexOf(day);
                        ss.setSpan(new ForegroundColorSpan(0xffF21B03), start, start + day.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        String day = Utils.getDay(bean.info.endTime);
                        String s = String.format(getString(R.string.retry_next_time), day);
                        ss = new SpannableString(s);
                        int start = s.indexOf(day);
                        ss.setSpan(new ForegroundColorSpan(0xffF21B03), start, start + day.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    binding.tip.setText(ss);
                } else {
                    binding.frame.frame.setVisibility(View.GONE);
                    binding.tip.setText(R.string.retry_next_time2);
                }
            }
        });
    }

    @Override
    protected void countDownTimeout() {
        apply();
    }

    private void apply() {
        if (Constant.white) {
            Utils.setStatus(10);
            SPHelper.getInstance(getActivity()).remove("white_refuse", "white_refuse_set").commit();
            ((MainActivity) getActivity()).getData();
        } else {
            ((MainActivity) getActivity()).showHome(0);
        }
    }

    @Override
    public void refresh() {
        update();
    }
}
